package main.java.maziacs.counters;

import java.util.*;

import main.java.maziacs.Dice;
import main.java.maziacs.Gem;
import main.java.maziacs.ITile;
import main.java.maziacs.characters.IHero;
import main.java.maziacs.squares.Square;
import main.java.maziacs.squares.White;


public class Prisoner extends Counter {
	
	private ITile tile;

	private IHero hero;
	private Integer numberPrisioners;
	
	public Prisoner(Bag bag){
		super(bag);
		tile=null;
		numberPrisioners=6;
	}

	public ITile getTile() {
		return tile;
	}

	public void setTile(ITile tile) {
		this.tile = tile;
	}
	
	
	public Integer getNumberPrisioners() {
		return numberPrisioners;
	}


	public void setNumberPrisioners(Integer numberPrisioners) {
		this.numberPrisioners = numberPrisioners;
	}
	
	
	
	public Square calculateDirection(){
		Dice d=new Dice(6);
		List<Square> l=new LinkedList<Square>();
		Square sq;
		Integer exits=0;
		if(getHero().equals(null)){
			
			Square[][] s=getTile().getSquares();
			
			if(s[0][1] instanceof White){
				l.add(s[0][1]);
				exits++;
			}
			if(s[1][0] instanceof White){
				l.add(s[1][0]);
				exits++;
			}
			if(s[1][2] instanceof White){
				l.add(s[1][2]);
				exits++;
			}
			if(s[2][1] instanceof White){
				l.add(s[2][1]);
				exits++;
			}
			sq=l.get(d.rollingDice()%exits);
			White w=(White)sq;
			w.setGem(new Gem(tile.getBoard().getGame()));
			getTile().getBoard().getGame().setNumberOfGems(getTile().getBoard().getGame().getNumberOfGems()-1);
			getTile().getBoard().getPath().getPath().add(getTile());
		}
		else{
			Square[][] s=getHero().getPosition().getTile().getSquares();
			
			if(s[0][1] instanceof White){
				l.add(s[0][1]);
				exits++;
			}
			if(s[1][0] instanceof White){
				l.add(s[1][0]);
				exits++;
			}
			if(s[1][2] instanceof White){
				l.add(s[1][2]);
				exits++;
			}
			if(s[2][1] instanceof White){
				l.add(s[2][1]);
				exits++;
			}
			sq=l.get(d.rollingDice()%exits);
			getHero().putGem();
		}
		return sq;
	}

	public void setHero(IHero hero) {
		this.hero = hero;
	}

	public IHero getHero() {
		return hero;
	}
	public boolean equals(Object o){
		boolean res=false;
		if(o instanceof Prisoner){
			Prisoner p=(Prisoner)o;
			if(getTile().equals(p.getTile()))
				res=true;
		}
		return res;
	}
}

		


